<?php
namespace Home\Controller;
use Common\Controller\AuthController;
use Think\Controller;

class PayController extends AuthController {
	private $jiqiguanli = 'jiqiguanli';
	public function index() {
		$this->display();
	}

	//我的机器
	public function machine() {
		if (IS_POST) {
			$where['uid'] = session('auth.id');
			echo $this->where2($where, 'id desc', I('post.page'));
		} else {
			$this->display();
		}

	}
	//新增机器||更新
	public function addMachine() {
		if (IS_POST) {
			//id存在即更新
			$id = I('get.id');
			$this->PublicDataModel(50, $id ? $id : '', U('Pay/machine'));
		} else {
			$code = I('get.code');
			if ($code) {
				$map['jiqima'] = $code;

				$t = M($this->jiqiguanli)->where($map)->getField('jiqima');
				if ($t) {
					echo 0;
				} else {
					echo 1;
				}
			} else {
				$id = I('get.id');
				if ($id) {
					$this->info = M($this->jiqiguanli)->where(array('id' => $id))->find();
				}
				$this->display();
			}

		}

	}
	//使用详情
	public function balance() {
		if (IS_POST) {
			$sid = session('auth.id');
			$map['uid'] = $sid;

			$list = M($this->jiqiguanli)->where($map)->field('jiqima')->select();
			if (!$list) {
				exit(json_encode(array('count' => 0, 'list' => 0, 'page' => 0)));
			}
			for ($i = 0; $i < count($list); $i++) {
				$ar[$i] = $list[$i]['jiqima'];
			}
			$str_array = array_string($ar);
			$where['code'] = array('in', $str_array);
			$where['type'] = 0;
			echo $this->where($where, 'id desc', I('post.page'));
		} else {
			$this->display();
		}

	}
	//历史记录
	public function history() {
		if (IS_POST) {
			$sid = session('auth.id');
			$map['uid'] = $sid;
			$list = M($this->jiqiguanli)->where($map)->field('jiqima')->select();
			if (!$list) {
				exit(json_encode(array('count' => 0, 'list' => 0, 'page' => 0)));
			}
			for ($i = 0; $i < count($list); $i++) {
				$ar[$i] = $list[$i]['jiqima'];
			}
			$str_array = array_string($ar);
			$where['code'] = array('in', $str_array);
			$where['type'] = 1;
			echo $this->where($where, 'id desc', I('post.page'));
		} else {
			$this->display();
		}
	}
	//设置
	public function system() {
		$this->display();
	}
	//生成二维码
	public function code() {
		$this->display();
	}
	//选择支付方式（这一步，不能设置权限，所以要改地方）
	public function pay() {
		$id = I('get.id');
		if (!$id) {
			$this->error('非法操作');
		}
		$this->display();
	}
	private function where($map, $desc, $p) {
		$pag = 5;
		$list = M('pay')
			->page($p . ',' . $pag)
			->field(true)
			->where($map)
			->order($desc)
			->select();
		foreach ($list as $key => $value) {
			$list[$key]['open_time'] = date('Y-m-d H:i:s', $list[$key]['open_time']);
			$list[$key]['end_time'] = date('Y-m-d H:i:s', $list[$key]['end_time']);
		}
		$count = M('pay')->where($map)->count();
		$data['count'] = ceil($count / $pag); //总页灵敏
		$data['list'] = $list;
		$data['page'] = $p; //当前请求页
		$data['money'] = M('pay')->where($map)->Sum('money');
		return json_encode($data, true);
	}
	private function where2($map, $desc, $p) {
		$pag = 5;
		$list = M($this->jiqiguanli)
			->page($p . ',' . $pag)
			->field(true)
			->where($map)
			->order($desc)
			->select();
		foreach ($list as $key => $value) {
			$list[$key]['url'] = U('code', array('code' => $list[$key]['jiqima']));
			$list[$key]['romUrl'] = U('Pay/addMachine', array('id' => $list[$key]['id']));
			$list[$key]['location'] = "http://m.amap.com/navi/?dest={$list[$key]['location']}&destName={$list[$key]['address']}&hideRouteIcon=1&key=565c8ec0d43b131f3795cf56eed64a79";
		}
		$count = M($this->jiqiguanli)->where($map)->count();
		$data['count'] = ceil($count / $pag); //总页灵敏
		$data['list'] = $list;
		$data['page'] = $p; //当前请求页
		return json_encode($data, true);
	}
}